
// 顶部
$(function () {
    $.ajax({
        type: "GET",
        url: "http://pk.tebieshengqian.com/get_catelog",
        dataType: "json",
        success: function (data) {
            var data = data.result.list
            var str = ''
            $.each(data, function (i, obj) {
                str += '<div class="inner_nav" style="display: inline-block;float: left">';
                str += '<span style="margin-right: 13px">' + obj.opt_name + '</span>';
                str += '</div>';
            });
            $("#inner").append(str);
            $.each(data, function (index, obj) {
                var Sub = data[index].sub;
                var str = '';
                if (index != 0 ){
                    str += '<div style="display: inline-block">'
                    str += '<img src="'+ Sub[0].opt_img +'" style="width: 50px;height: 45px;margin:30px 0 0 55px"/>'
                    str += '<span style="display: block;margin-left:55px">'+ obj.opt_name +'</span>'
                    str += '</div>'
                }

                $('#mydiv').append(str);

            })

        }
    })
})

    //  轮播图
$(function () {
    $.ajax({
        type: "GET",
        url: "http://pk.tebieshengqian.com/get_banner",
        dataType: "json",
        success: function (result) {
            var result = result.result.banner
            var str="<ul id='banner' style='width: 1600px;height: 200px;position: absolute;left: 0;'>";
            $.each(result,function(i,n){
                str+= '<li style="float: left;height: 200px; list-style: none;"><img src="'+ n.pic + '" style="width: 400px;height: 200px;">';
            });
            str+="</ul>";
            $(".cont").append(str);

            }

    })
})

    //商品列表
$(function () {
    $.ajax({
        type: "GET",
        url: "http://pk.tebieshengqian.com/cms_search",
        dataType: "json",
        success: function (ret) {
            console.log(ret);
            var ret = ret.result.goods_list
            var str = '<ul style="margin: 20px auto 0">'
            $.each(ret, function (i, obj) {
                str += '<li>';
                str += '<a href="#">'
                str += '<span style="width: 126px;height: 126px;"><img src="'+ obj.goods_thumbnail_url+'" style="padding:10px;width: 126px;height: 126px;"/></span>'
                str += '<div style="display: inline;float: right;width: 60%;color: black">'
                str += '<span style="font-size: 13px">' + obj.goods_name + '</span>'
                str += '<div style="margin-top: 45px;color: #C9C9C9">'
                str += '<span style="text-decoration:line-through">'+ '原价:'+ obj.min_normal_price +' '+'分</span>'
                str += '<span style="margin-left: 15px">'+ '已售:'+ obj.sold_quantity +' '+'件</span>'
                str += '</div>'
                str += '<div>'
                str += '<span style="color: #fa435c;font-weight: bold;">'+ '现价:'+ obj.min_group_price +' '+'分</span>'
                str += '<span style="margin-left: 15px">'+ '优惠券:'+ obj.coupon_discount +' '+'分</span>'
                str += '</div>'
                str += '</div>'
                str += '</a>'
                str += '</li>';
            });
            str +="</ul>";
                $("#page_size").append(str);
                },

    })
})


//    顶部滑动

    horizontalMove({
        innerId: "#inner",
        warpId : "#warp",
        speed: 0.5
    });
    function horizontalMove(options){
            var speed = 0.5;
            var startX;
            var x = 0;
            var aboveX=0;

        options = options ||{};
        speed = options.speed;

        if(options.innerId && options.warpId){
            var documentWidth = $(options.innerId).width();
            var wapperWidth = $(options.warpId).width();
            var inner = $(options.innerId);
            var warp = $(options.warpId)[0]

            function touchStart(e){
                e.preventDefault();
                var touch = e.touches[0];
                startX = touch.pageX;
                console.log(startX);
            }

            function touchMove(e){
                e.preventDefault();
                var  touch = e.touches[0];
                x = touch.pageX - startX;
                inner.css({left: aboveX + x * speed });
            }

            function touchEnd(e){
                aboveX = parseInt(inner.css("left"));
                if(x>0 && aboveX > 0){
                    inner.animate({left: 0 }, 200);
                    aboveX = 0;
                }

                if(x<0 && (aboveX < (-(documentWidth - wapperWidth)))) {
                    inner.animate({left: -(documentWidth - wapperWidth)}, 200);
                    aboveX= -(documentWidth - wapperWidth);
                    console.log(aboveX);
                }
            }
            warp.addEventListener('touchstart', touchStart,false);
            warp.addEventListener('touchmove', touchMove,false);
            warp.addEventListener('touchend', touchEnd,false);

        }
    }


//    点击出现弹窗
        var mydiv = $("#mydiv");
        $(function () {
            $("#pto").click(function (e) {
                e.stopPropagation();
                mydiv.stop().fadeToggle();
                $("#mydiv").one("click", function () {
                    mydiv.stop().hide();
                });
                $(document).one("click", function () {
                    mydiv.stop().hide();
                });
                mydiv.click(function (e) {
                    e.stopPropagation();
                });
            });

        })

//图片轮播

    // $(function () {
    //     var index = 0;
    //     var timer = setInterval(fn, 1500)
    //
    //     function fn() {
    //         index++;
    //         if (index> $(".cont ul li").length-1){
    //             index =0
    //             $(".cont ul li").css({
    //                 left:0
    //             })
    //         }
    //         $(".cont ul").animate({
    //             left: -index*400
    //         }, 1500)
    //     }
    //
    // })

